package com.zhongen.oa.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongen.common.core.util.R;
import com.zhongen.common.log.annotation.SysLog;
import com.zhongen.oa.entity.BpmFormEntity;
import com.zhongen.oa.service.BpmFormService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工作流表单
 *
 * @author Code Generator
 * @date 2024-01-25 16:14:21
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/bpmForm")
@Tag(description = "bpmForm", name = "工作流表单管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BpmFormController {

	private final BpmFormService bpmFormService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param bpmForm 工作流表单
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('zhongen_bpmForm_view')")
	public R getBpmFormPage(@ParameterObject Page page, @ParameterObject BpmFormEntity bpmForm) {
		LambdaQueryWrapper<BpmFormEntity> wrapper = Wrappers.lambdaQuery();
		return R.ok(bpmFormService.page(page, wrapper));
	}

	/**
	 * 通过id查询工作流表单
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('zhongen_bpmForm_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(bpmFormService.getById(id));
	}

	/**
	 * 新增工作流表单
	 * @param bpmForm 工作流表单
	 * @return R
	 */
	@Operation(summary = "新增工作流表单", description = "新增工作流表单")
	@SysLog("新增工作流表单")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('zhongen_bpmForm_add')")
	public R save(@RequestBody BpmFormEntity bpmForm) {
		return R.ok(bpmFormService.save(bpmForm));
	}

	/**
	 * 修改工作流表单
	 * @param bpmForm 工作流表单
	 * @return R
	 */
	@Operation(summary = "修改工作流表单", description = "修改工作流表单")
	@SysLog("修改工作流表单")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('zhongen_bpmForm_edit')")
	public R updateById(@RequestBody BpmFormEntity bpmForm) {
		return R.ok(bpmFormService.updateById(bpmForm));
	}

	/**
	 * 通过id删除工作流表单
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除工作流表单", description = "通过id删除工作流表单")
	@SysLog("通过id删除工作流表单")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('zhongen_bpmForm_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(bpmFormService.removeBatchByIds(CollUtil.toList(ids)));
	}

	/**
	 * 导出excel 表格
	 * @param bpmForm 查询条件
	 * @param ids 导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('zhongen_bpmForm_export')")
	public List<BpmFormEntity> export(BpmFormEntity bpmForm, Long[] ids) {
		return bpmFormService
			.list(Wrappers.lambdaQuery(bpmForm).in(ArrayUtil.isNotEmpty(ids), BpmFormEntity::getId, ids));
	}

}
